using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Read.Models;
using Read.Services;
using Read.Utility;

namespace Read.Web.Admin
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public class UserController : AdminBaseController
    {
        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index(QueryParamsAdmin query)
        {
            if (HttpContext.Request.IsAjaxRequest())
            {
                var srevice = new AdminService();
                var res = srevice.getAdminList(query);
                // var total = adminsrevice.getadmincount(query);
                // var adminlist = adminsrevice.getadminlist(query);
                // var res = new ResultData<List<AdminModel>>()
                // {
                //     Result = true,
                //     Total = total,
                //     Data = adminlist
                // };
                return Json(res);
            }
            else
            {
                ViewBag.Title = "用户列表";
                return View();
            }
        }

        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add(AdminModel model)
        {
            var srevice = new AdminService();
            var add = srevice.adminAdd(model);
            var res = new ResultData<long>()
            {
                Result = true,
                Data = add,
                Message = "添加用户成功"
            };
            return ResultJSON(res.Result, res, res.Message);
            //return Json(res);
        }

        /// <summary>
        /// 用户编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult Edit(AdminModel model)
        {
            var srevice = new AdminService();
            var edit = srevice.adminEdit(model);
            var res = new ResultData<bool>()
            {
                Result = true,
                Data = edit,
                Message = "编辑用户成功"
            };
            return ResultJSON(res.Result, res, res.Message);
            //return Json(res);
        }

        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Del(int id)
        {
            var srevice = new AdminService();
            var del = srevice.adminDel(id);
            var res = new ResultData<bool>()
            {
                Result = true,
                Data = del,
                Message = "删除用户成功"
            };
            return ResultJSON(res.Result, res, res.Message);
            //return Json(res);
        }
    }
}